# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        潘炎珍  
# Name:         get_info
# Description:
# Author:       98745
# Date:         2019/4/9
#-------------------------------------------------------------------------------

import requests
from bs4 import *
import re
from projects.boss_spyder.db_manage import *

def split_job(string):
    ex = '经验不限|1年以内|\d\-\d+年|应届生'
    ed = '学历不限|高中|大专|本科|硕士|博士'
    try:
        ss={}
        experience = re.search(ex,string).group()
        # print(experience)
        n = string.find(experience)
        place = string[0:n]
        # print(place)
        education = re.search(ed,string).group()
        # print(education)
        ss['place']=place
        ss['experience']=experience
        ss['education']=education
        if len(ss)==3:
            return ss
    except Exception as e:
        print(ss)
        print(e)


def getinfo(n):
    try:
        url='https://www.zhipin.com/c101280100/?query=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%B8%88&page={}&ka=page-{}'.format(n,n)
        header={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
        response=requests.get(url,headers=header)
        soup=BeautifulSoup(response.text,'lxml')
        soup_div=soup.find('div',{'class':"job-list"})
        soup_li=soup_div.find_all('li')
        data={}
        for info in soup_li:
            job=info.find('div',{'class':"info-primary"}) #获取与工作有关的信息
            position=job.find('div',{'class':"job-title"}).text #获取职位
            data['position']=position
            salary=job.find('span').text #获取工资
            data['salary']=salary
            string=job.p.text
            sj=split_job(string) #获取工作地点和学历、经历要求
            data.update(sj)
            firm=info.find('div',{'class':"info-company"}) #获取与公司有关的信息
            company=firm.a.text #获取公司名称
            data['company']=company
            character=firm.p.text #获取司的性质、是否上市和公司规模
            data['character']=character
            if len(data)==7:
                insert(db,data)
    except Exception as e:
        print(data)
        print(e)


if __name__ == "__main__":
    # count=1
    for i in range(0,10):
        n=i+1
        getinfo(n)











